package com.abc.robot01

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import com.abc.robot01.activity.px
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.vivemodel.Robot
import org.json.JSONObject

@Composable
fun Base64QrCodeImage(base64QrCode: String) {
    // 解码 Base64 为 Bitmap
    val bitmap = decodeBase64ToBitmap(base64QrCode)

    // 如果 Bitmap 不为空，则显示二维码
    bitmap?.let {
        Image(
            bitmap = it.asImageBitmap(),
            contentDescription = "QR Code",
            modifier = Modifier.size(297.87.px) // 这里你可以调整二维码的大小
        )
    }
}

//suspend fun getToken() {
//    Log.e("RetrofitService'", "开始获取 access_token")
//    val res = RetrofitService.instance.getToken(Robot.robotId)
//    Log.e("RetrofitService'", "access_token:"+res)
//    val token = res.data.access_token
//    Log.e("RetrofitService'", "access_token:$token")
//    Robot.accessToken = token
//}

fun decodeBase64ToBitmap(base64Str: String): Bitmap? {
    return try {
        val decodedBytes = Base64.decode(base64Str, Base64.DEFAULT)
        BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}


